﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ajax;
using SeDuce.BLL;
using SeDuce.Model;
using Resources;

public partial class Buy : RepeaterPagerBase
{
    Lazy_Yu<BrandBLL> lazyBrandBLL = new Lazy_Yu<BrandBLL>(() => new BrandBLL());
    public BrandBLL LazyBrandBLL
    {
        get { return this.lazyBrandBLL.Value; }
    }

    Lazy_Yu<ProductBLL> lazyProductBLL = new Lazy_Yu<ProductBLL>(() => new ProductBLL());
    public ProductBLL LazyProductBLL
    {
        get { return this.lazyProductBLL.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Buy));
        BindCategories();
        if (!IsPostBack)
        {
            int productID = this.GetQueryString("productID", false).ToInt();
            SeDuce.Model.Product product = this.LazyProductBLL.GetProductByProductID(productID).ToList<SeDuce.Model.Product>().SingleOrDefault<SeDuce.Model.Product>();
            if (product != null)
            {
                this.imgImageBig.ImageUrl = ResolveUrl(product.ImageBig);
                this.literProductName.Text = HttpUtility.HtmlEncode(product.ProductName);

                decimal price = 0, saleprice = 0;
                if (System.Globalization.CultureInfo.CurrentCulture.Name.ToLower() == "zh-cn")
                {
                    price = product.UnitPrice;
                    saleprice = product.SalePrice;
                }
                else if (System.Globalization.CultureInfo.CurrentCulture.Name.ToLower() == "en-us")
                {
                    price = product.UnitPriceUS;
                    saleprice = product.SalePriceUS;
                }
                else if (System.Globalization.CultureInfo.CurrentCulture.Name.ToLower() == "ja-jp")
                {
                    price = product.UnitPriceJP;
                    saleprice = product.SalePriceJP;
                }
                this.lblPrice.Text = (Convert.ToInt32(price) != 0) ? string.Format("{0:C}", price) : ResourceGlobal.Product_Ask;
                this.lblPriceSubmit.Text = price.ToString();
                if (Convert.ToInt32(saleprice) != 0)
                {
                    this.lblPrice.Style["text-decoration"] = "line-through";
                    this.lblSalePrice.Text = string.Format("{0:C}", saleprice);
                    this.lblPriceSubmit.Text = saleprice.ToString();
                }
                this.literStatus.Text = new Utils().productStatus.Where<ProductStatus>(s => s.ID == product.Status).SingleOrDefault<ProductStatus>().Name;
                this.rptSizes.DataSource = product.Size.Split(',');
                this.rptSizes.DataBind();
                this.rptColor.DataSource = product.Color.Split(',');
                this.rptColor.DataBind();
            }
        }
    }

    protected void rptCategories_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "CATEGORY_CHANGE")
        {
            LinkButton lbtnCategory = (LinkButton)e.Item.FindControl("lbtnCategory");
            Response.Redirect(ResolveUrl("~/Brands/" + e.CommandArgument.ToString()));
        }
    }

    private void BindCategories()
    {
        this.rptCategories.DataSource = this.LazyBrandBLL.GetBrands().ToList<Brand>().OrderBy(t => t.Sort).ToList<Brand>();
        this.rptCategories.DataBind();
    }

    [Ajax.AjaxMethod()]
    public bool UpdateBrands()
    {
        try
        {
            BindCategories();
            return true;
        }
        catch { return false; }
    }
}